﻿using MvcLibrary.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminTravelHotelController : Controller
    {
        #region "Index"
        public ActionResult Index(int idTravelProduct, int idTravelCity)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                TravelHotelModel model = new TravelHotelModel();

                if (idTravelProduct > 0 && idTravelCity == 0)
                {
                    // Lấy danh sách các TravelCity nằm trong một TravelCountry vô SelecList               
                    foreach (var travelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == idTravelProduct))
                    {
                        model.listCategory.Add(new ListCategory()
                        {
                            Id = travelCity.Id,
                            Name = travelCity.Name
                        });
                        //lấy danh sách các Hotel 
                        foreach (var travelHotel in db.EntityTravelHotels.Where(o => o.IdTravelCity == travelCity.Id))
                        {
                            model.listTravelHotel.Add(new TravelHotel()
                            {
                                Id = travelHotel.Id,
                                IdTravelCity = travelHotel.IdTravelCity,
                                Name = travelHotel.Name,
                                Address = travelHotel.Address,
                                Phone = travelHotel.Phone,
                                Comment = travelHotel.Comment,
                                Rate = travelHotel.Rate,
                                Image = travelHotel.Image,
                                Promotion = Convert.ToBoolean(travelHotel.Promotion),
                                //SEO
                                Titletag = travelHotel.Titletag,
                                Meta_description = travelHotel.Meta_description,
                                Meta_keyword = travelHotel.Meta_keyword
                            });
                        }

                    }
                    //check idTravelCountry và idTravelCity
                    model.listCategoryId.IdTravelCity = idTravelCity;
                    model.listCategoryId.IdTravelProduct = idTravelProduct;

                    model.category = new SelectList(model.listCategory.ToArray(), "Id", "Name");
                    return View(model);
                }
                else if (idTravelProduct > 0 && idTravelCity > 0)
                {
                    // Lấy danh sách các TravelCity nằm trong một TravelCountry vô SelecList               
                    foreach (var travelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == idTravelProduct))
                    {
                        model.listCategory.Add(new ListCategory()
                        {
                            Id = travelCity.Id,
                            Name = travelCity.Name
                        });

                    }
                    model.category = new SelectList(model.listCategory.ToArray(), "Id", "Name");

                    //lấy danh sách các Hotel 
                    foreach (var travelHotel in db.EntityTravelHotels.Where(o => o.IdTravelCity == idTravelCity))
                    {
                        model.listTravelHotel.Add(new TravelHotel()
                        {
                            Id = travelHotel.Id,
                            IdTravelCity = travelHotel.IdTravelCity,
                            Name = travelHotel.Name,
                            Address = travelHotel.Address,
                            Phone = travelHotel.Phone,
                            Comment = travelHotel.Comment,
                            Rate = travelHotel.Rate,
                            Image = travelHotel.Image,
                            Promotion = Convert.ToBoolean(travelHotel.Promotion),
                            //SEO
                            Titletag = travelHotel.Titletag,
                            Meta_description = travelHotel.Meta_description,
                            Meta_keyword = travelHotel.Meta_keyword
                        });
                    }
                    //check idTravelCountry và idTravelCity
                    model.listCategoryId.IdTravelCity = idTravelCity;
                    model.listCategoryId.IdTravelProduct = idTravelProduct;

                    return View(model);
                }
                else if (idTravelProduct == 0 && idTravelCity > 0)
                {
                    //Dùng lấy id của TravelCountry
                    EntityTravelCity traveCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == idTravelCity);
                    // Lấy danh sách các TravelCity nằm trong một TravelCountry vô SelecList
                    foreach (var travelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == traveCity.IdTravelProduction))
                    {
                        model.listCategory.Add(new ListCategory()
                        {
                            Id = travelCity.Id,
                            Name = travelCity.Name
                        });
                    }
                    model.category = new SelectList(model.listCategory.ToArray(), "Id", "Name");
                    //lấy danh sách các Hotel 
                    foreach (var travelHotel in db.EntityTravelHotels.Where(o => o.IdTravelCity == Convert.ToInt32(idTravelCity)))
                    {
                        model.listTravelHotel.Add(new TravelHotel()
                        {
                            Id = travelHotel.Id,
                            IdTravelCity = travelHotel.IdTravelCity,
                            Name = travelHotel.Name,
                            Address = travelHotel.Address,
                            Phone = travelHotel.Phone,
                            Comment = travelHotel.Comment,
                            Rate = travelHotel.Rate,
                            Image = travelHotel.Image,
                            Promotion = Convert.ToBoolean(travelHotel.Promotion),
                            //SEO
                            Titletag = travelHotel.Titletag,
                            Meta_description = travelHotel.Meta_description,
                            Meta_keyword = travelHotel.Meta_keyword
                        });
                    }
                    //check idTravelCountry và idTravelCity
                    model.listCategoryId.IdTravelCity = idTravelCity;
                    model.listCategoryId.IdTravelProduct = idTravelProduct;

                    return View(model);
                }
                return View(model);
            }

        }

        [HttpPost]
        public ActionResult Index(TravelHotelModel model, string idTravelCity, int idTravelProduct = 0)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //Dùng lấy id của TravelCountry
                EntityTravelCity traveCity = db.EntityTravelCities.FirstOrDefault(o => o.Id == Convert.ToInt32(idTravelCity));
                // Lấy danh sách các TravelCity nằm trong một TravelCountry vô SelecList
                foreach (var travelCity in db.EntityTravelCities.Where(o => o.IdTravelProduction == traveCity.IdTravelProduction))
                {
                    model.listCategory.Add(new ListCategory()
                    {
                        Id = travelCity.Id,
                        Name = travelCity.Name
                    });
                }
                model.category = new SelectList(model.listCategory.ToArray(), "Id", "Name");

                //lấy danh sách các Hotel 
                foreach (var travelHotel in db.EntityTravelHotels.Where(o => o.IdTravelCity == Convert.ToInt32(idTravelCity)))
                {
                    model.listTravelHotel.Add(new TravelHotel()
                    {
                        Id = travelHotel.Id,
                        IdTravelCity = travelHotel.IdTravelCity,
                        Name = travelHotel.Name,
                        Address = travelHotel.Address,
                        Phone = travelHotel.Phone,
                        Comment = travelHotel.Comment,
                        Rate = travelHotel.Rate,
                        Image = travelHotel.Image,
                        Promotion = Convert.ToBoolean(travelHotel.Promotion),
                        //SEO
                        Titletag = travelHotel.Titletag,
                        Meta_description = travelHotel.Meta_description,
                        Meta_keyword = travelHotel.Meta_keyword
                    });
                }

                //check idTravelCountry và idTravelCity
                model.listCategoryId.IdTravelProduct = idTravelProduct;
                model.listCategoryId.IdTravelCity = Convert.ToInt32(idTravelCity);

                return View(model);
            }
        }
        #endregion //edit

        #region "Delete"
        public string Delete(int id)
        {
            try
            {
                dbVietPremierDataContext db = new dbVietPremierDataContext();

                EntityTravelHotel item = db.EntityTravelHotels.Where(p => p.Id == id).Single();
                if (item.Image != null)
                {
                    string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/travelhotels/"), item.Image); // file hinh cu
                    System.IO.File.Delete(fileToDelete);
                }
                db.EntityTravelHotels.DeleteOnSubmit(item);
                db.SubmitChanges();
                return "Ok";

            }
            catch
            {
                return "Error";
            }
        }
        #endregion //delete

        #region "Insert"
        public ActionResult Insert(string idTravelCity, int idTravelProduct)
        {
            TravelHotel model = new TravelHotel();
            model.IdTravelCity = Convert.ToInt32(idTravelCity);
            model.listCategoryId.IdTravelCity = Convert.ToInt32(idTravelCity);
            model.listCategoryId.IdTravelProduct = idTravelProduct;
            return View(model);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(int idTravelProduct, TravelHotel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityTravelHotel travelHotel = new EntityTravelHotel();
                travelHotel.IdTravelCity = model.IdTravelCity;
                travelHotel.Name = model.Name;
                travelHotel.Address = model.Address;
                travelHotel.Phone = model.Phone;
                travelHotel.Comment = model.Comment;
                travelHotel.Rate = model.Rate;
                travelHotel.Promotion = false;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/travelhotels/"), encodestring);
                    Image.SaveAs(path);

                    travelHotel.Image = encodestring;
                }
                //<!--SEO -->
                travelHotel.Titletag = model.Titletag;
                travelHotel.Meta_description = model.Meta_description;
                travelHotel.Meta_keyword = model.Meta_keyword;

                db.EntityTravelHotels.InsertOnSubmit(travelHotel);
                db.SubmitChanges();

                //int Count = db.EntityTravelHotels.Count() - 1 ;
                //int newTravelHotelID = db.EntityTravelHotels.Skip(Count).FirstOrDefault().Id;
                return RedirectToAction("Index", "AdminTravelHotel", new { idTravelProduct = idTravelProduct, idTravelCity = model.IdTravelCity });
            }
        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int idTravelHotel, int idTravelCity, int idTravelProduct)
        {
            TravelHotel model = new TravelHotel();
            model.listCategoryId.IdTravelCity = idTravelCity;
            model.listCategoryId.IdTravelProduct = idTravelProduct;
            return View(model);
        }

        [HttpPost]
        public JsonResult GetDetail(int idTravelHotel)
        {
            TravelHotel model = new TravelHotel();
            model.Id = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityTravelHotel travelHotel = db.EntityTravelHotels.FirstOrDefault(o => o.Id == idTravelHotel);

                if (travelHotel != null)
                {
                    model.Id = travelHotel.Id;
                    model.IdTravelCity = travelHotel.IdTravelCity;
                    model.Name = travelHotel.Name;
                    model.Address = travelHotel.Address;
                    model.Phone = travelHotel.Phone;
                    model.Comment = travelHotel.Comment;
                    model.Image = travelHotel.Image;
                    model.Rate = travelHotel.Rate;
                    //<!--SEO -->
                    model.Titletag = travelHotel.Titletag;
                    model.Meta_description = travelHotel.Meta_description;
                    model.Meta_keyword = travelHotel.Meta_keyword;

                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int idTravelHotel, int idTravelCity, int idTravelProduct, TravelHotel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntityTravelHotel travelHotel = db.EntityTravelHotels.Where(p => p.Id == idTravelHotel).FirstOrDefault();
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (travelHotel.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/travelhotels/"), travelHotel.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/travelhotels/"), encodestring);
                    Image.SaveAs(path);

                    travelHotel.Image = encodestring;
                }
                travelHotel.Name = model.Name;
                travelHotel.Address = model.Address;
                travelHotel.Phone = model.Phone;
                travelHotel.Comment = model.Comment;
                travelHotel.Rate = model.Rate;
                //<!--SEO -->
                travelHotel.Titletag = model.Titletag;
                travelHotel.Meta_description = model.Meta_description;
                travelHotel.Meta_keyword = model.Meta_keyword;

                db.SubmitChanges();
                return RedirectToAction("Index", "AdminTravelHotel", new { idTravelProduct = idTravelProduct, idTravelCity = model.IdTravelCity });
            }
        }
        #endregion //edit
    }
}
